﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace ERPStore.Models
{
	/// <summary>
	/// Filtre pour la recherche
	/// </summary>
	[Serializable]
	[DataContract]
	public class ProductListFilter
	{
		public ProductListFilter()
		{
			ListType = ProductListType.All;
			SortList = new List<SortProperty>();
			RemoveParentVariation = false;
			SortByScore = false;
		}

		/// <summary>
		/// Liste des propriétés etendues
		/// </summary>
		/// <value>The extended parameters.</value>
		public NameValueCollection ExtendedParameters { get; set; }
		/// <summary>
		/// Id de la catégorie
		/// </summary>
		/// <value>The product category id.</value>
		[DataMember]
		public int? ProductCategoryId { get; set; }
		[IgnoreDataMember]
		public Models.ProductCategory ProductCategory { get; set; }
		/// <summary>
		/// Id de la marque
		/// </summary>
		/// <value>The brand id.</value>
		[DataMember]
		public int? BrandId { get; set; }
		[IgnoreDataMember]
		public Models.Brand Brand { get; set; }
		/// <summary>
		/// Identifiant du contact
		/// </summary>
		/// <value>The user id.</value>
		[IgnoreDataMember]
		public int? UserId { get; set; }
		/// <summary>
		/// Identifiant de la société
		/// </summary>
		/// <value>The corporate id.</value>
		[IgnoreDataMember]
		public int? CorporateId { get; set; }
		/// <summary>
		/// Terme de recherche
		/// </summary>
		/// <value>The search.</value>
		[DataMember]
		public string Search { get; set; }
		/// <summary>
		/// Type de liste
		/// </summary>
		/// <value>The type of the list.</value>
		[DataMember]
		public ProductListType ListType { get; set; }
		/// <summary>
		/// Identifiant d'une selection prédéfinie.
		/// </summary>
		/// <value>The selection id.</value>
		[DataMember]
		public int? SelectionId { get; set; }
		/// <summary>
		/// Nom d'une selection
		/// </summary>
		[DataMember]
		public string SelectionName { get; set; }
		/// <summary>
		/// Liste des ordres de tri
		/// </summary>
		/// <value>The sort list.</value>
		[DataMember]
		public List<SortProperty> SortList { get; private set; }
		/// <summary>
		/// Indique si le produit est en stock
		/// </summary>
		[DataMember]
		public bool? HasStock { get; set; }
		/// <summary>
		/// Indique si le produit possède une photo
		/// </summary>
		/// <value>The has picture.</value>
		[DataMember]
		public bool? HasPicture { get; set; }

		/// <summary>
		/// Restriction sur les frais de port gratuits
		/// </summary>
		[DataMember]
		public bool? ShippingFree { get; set; }

		/// <summary>
		/// Prix minimum du produit
		/// </summary>
		[DataMember]
		public int? MinSalePrice { get; set; }

		/// <summary>
		/// Prix maximum des produits
		/// </summary>
		[DataMember]
		public int? MaxSalePrice { get; set; }

		/// <summary>
		/// Indique qu'il s'agit d'une variation parente
		/// </summary>
		[DataMember]
		public bool RemoveParentVariation { get; set; }

		/// <summary>
		/// Renvoie les produits variants dans le resultat de recherche
		/// </summary>
		[DataMember]
		public bool RemoveVariant { get; set; }

		/// <summary>
		/// commutateur utilisé dans le cas d'une recherche
		/// </summary>
		[DataMember]
		public bool SortByScore { get; set; }

		[DataMember]
		public int PageIndex { get; set; }

		[DataMember]
		public int PageSize { get; set; }

		public bool IsEmpty()
		{
			return !this.BrandId.HasValue
					&& !this.CorporateId.HasValue
					&& !this.HasStock.HasValue
					&& !this.ProductCategoryId.HasValue
					&& string.IsNullOrEmpty(this.Search)
					&& !this.SelectionId.HasValue
					&& !this.UserId.HasValue;
		}

		public override string ToString()
		{
			var result = new StringBuilder();
			result.AppendLine();
			result.AppendFormat("ProductCategoryId : {0}{1}", ProductCategoryId, Environment.NewLine);
			result.AppendFormat("BrandId : {0}{1}", BrandId, Environment.NewLine);
			result.AppendFormat("CorporateId : {0}{1}", CorporateId, Environment.NewLine);
			result.AppendFormat("UserId : {0}{1}", UserId, Environment.NewLine);
			result.AppendFormat("HasStock : {0}{1}", HasStock, Environment.NewLine);
			result.AppendFormat("Search : {0}{1}", Search, Environment.NewLine);
			result.AppendFormat("ListType : {0}{1}", ListType, Environment.NewLine);
			if (ExtendedParameters != null)
			{
				foreach (string item in ExtendedParameters)
				{
					result.AppendFormat("{0} : {1}{2}", item, ExtendedParameters[item], Environment.NewLine);
				}
			}
			return result.ToString();
		}
	}
}
